import axios from 'axios';
import { ElMessage } from 'element-plus';

import { srvHost } from '@/config/Host';

import { getAdminToken } from './setToken';

const fetchData = async (url: string, data?: any) => {
    const headers: any = {
        'Content-Type': 'application/json;charset=utf-8'
    }
    const token = getAdminToken()
    if (token) headers['token'] = token
    const res = await axios.request({
        baseURL: srvHost + url,
        method: 'post',
        headers,
        data: data
    }).catch(err => {
        console.log(err)
        try {
            ElMessage.error(err.response.data.message)
        } catch(_err) {
            ElMessage.error('未知错误')
        }
    })
    if (res) {
        const result = res.data
        return result.data
    } else {
        return undefined
    }
}

export default fetchData